package com.test.springboot.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

public class S38 {
    public static void main(String[] args) {
        //创建线程池
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("lyt"+"-%d").build();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,10,0L,
                TimeUnit.SECONDS,new LinkedBlockingQueue<>(1024),threadFactory);

        for (int i = 0; i < 20; i++) {
            executor.execute(()-> System.out.println(DateUtilSafe.parse("2019-06-01 16:34:30")));
        }
    }
    public static class DateUtilNotSafe {

        private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        public static Date parse(String date) {
            Date parse = null;
            try {
                parse = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return parse;
        }
    }
    public static class DateUtilSafe {
       private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = ThreadLocal
               .withInitial(()->new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
       public static Date parse(String dateStr) {
           Date date = null;
           try {
               THREAD_LOCAL.get().parse(dateStr);
           } catch (ParseException e) {
               e.printStackTrace();
           }
           return date;
       }


    }
}
